package com.ashin.controller;

import com.ashin.bean.Car;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

@Controller
public class HibernateValidatorController {


    @RequestMapping("/valid")
    public String paramValidator(@Valid Car car, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for (ObjectError allError : allErrors) {
                FieldError fieldError = (FieldError) allError;

                System.out.println(fieldError.getField() + ":" + allError.getDefaultMessage());
            }
        }

        return "views/success";
    }
}
